package com.wtwd.campus.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.dao.ClassMapper;
import com.wtwd.campus.dao.GradeMapper;
import com.wtwd.campus.entity.Classes;
import com.wtwd.campus.entity.Grade;
import com.wtwd.campus.model.vo.GradeVO;
import com.wtwd.campus.model.vo.ResultVO;
import com.wtwd.campus.service.GradeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author mjy
 * @date 2021/06/17
 * @description
 */
@Service
public class GradeServiceImpl implements GradeService {
    @Resource
    private GradeMapper gradeMapper;

    @Resource
    private ClassMapper classMapper;

    @Override
    public List<Map<String, Object>> getGradeIdAndGradeNameBySchoolId(Integer schoolId) {
        return gradeMapper.getGradeIdAndGradeNameBySchoolId(schoolId);
    }

    @Override
    public IPage<GradeVO> queryPage(Page page, List<Integer> schoolIds) {
        if (schoolIds == null) {
            return null;
        }
        return gradeMapper.queryList(page, schoolIds);
    }

    /**
     * 如果该年级下存在班级,无法删除
     *
     * @param id
     * @return
     */
    @Override
    public ResultVO deleteGradeById(Integer id) {
        Integer count = new LambdaQueryChainWrapper<>(classMapper)
                .eq(Classes::getGradeId, id)
                .count();
        if (count > 0) {
            return ResultVO.error(RespCode.GRADE_EXIST_CLASS);
        }
        gradeMapper.deleteById(id);
        return ResultVO.ok();
    }

    @Override
    public ResultVO createGradeBatch(Grade grade, String gradeNames) {
        if (StringUtils.isBlank(gradeNames)) {
            return ResultVO.error(RespCode.PARAMS_ERROR);
        }
        ArrayList<Grade> addDatas = null;
        try {
            String[] split = gradeNames.split(",");
            addDatas = new ArrayList<>(split.length);
            long now = System.currentTimeMillis();
            for (int i = 0; i < split.length; i++) {
                Grade grade1 = new Grade();
                grade1.setGradeName(split[i]);
                grade1.setSchoolId(grade.getSchoolId());
                grade1.setCreateTime(now);
                grade1.setUpdateTime(now);
                addDatas.add(grade1);
            }
            gradeMapper.insertBatch(addDatas);
        } catch (NumberFormatException e) {
            return ResultVO.error(RespCode.PARAMS_ERROR);
        } catch (DuplicateKeyException e) {
            return ResultVO.error(RespCode.GRADE_EXIST);
        }
        return ResultVO.ok();
    }

    @Override
    public List<Grade> listGradeBySchoolIds(List<Integer> schoolIds) {
        return gradeMapper.listGradeBySchoolIds(schoolIds);
    }

    @Override
    public Grade getGradeByGradeNameAndSchoolId(Integer schoolId, String gradeName) {
        return new LambdaQueryChainWrapper<>(gradeMapper)
                .eq(Grade::getSchoolId, schoolId)
                .eq(Grade::getGradeName, gradeName)
                .one();
    }

    @Override
    public Grade save(Grade grade) {
        gradeMapper.insert(grade);
        return grade;
    }


}
